<?php


namespace Home\Controller;


use Think\Controller;

class ListPageController extends BaseController
{

    public function index(){
        $Conf = ['title'=>'列表页','js'=>['list']];
        $this->assign('Conf',$Conf);

        $cid=i('get.cid');

        //1.处理面包屑
        $fatherData=$this->getFather(m('category')->select(),$cid);
       //数组的翻转
        $fatherData = array_reverse($fatherData);
        $this->assign('fatherData',$fatherData);
        //2.实现筛选
        //查找当前分类以及自己数据
        $cids = $this->getSon(m('category')->select(),$cid);
        $cids[] = $cid;
        $gids = m('goods')->where("cid in (".implode(',',$cids).")")->getField('gid',true);
//        dd($gids);
        if ($gids){

            //关联商品属性表和类型属性表
            $field=m('goods_attr')->alias('ga')
                ->join('__TYPEATTRIBUTE__ ta on ga.taid=ta.taid')
                ->where("ga.gid in (".implode(',',$gids).")")
                ->group('ga.gtvalue')->select();
            //dd($field);
            //数组的重组  对$field进行重组，taid一样的放一起
            $temp = [];
            foreach($field as $k=>$v)
            {
                $temp[$v['taid']][] = $v;
            }
//            dd($temp);
            $finalTemp=[];
            foreach ($temp as $k=>$v){
                $finalTemp[] =[
                    'name'=>m('typeattribute')->where("taid={$k}")->getField('tname'),
                    'value'=>$v
                ];
            }


            $this->assign('finalTemp',$finalTemp);
//            dd($finalTemp);
            $param = isset($_GET['param']) ? explode('_',$_GET['param']) : array_fill(0,count($finalTemp),0);
//            dd($param);
            $this->assign('param',$param);
            foreach($param as $k=>$v)
            {
                if($v)
                {//说明有筛选动作
                    $gtvalue = m('goods_attr')->where("gaid={$v}")->getField('gtvalue');
//                    dd($gtvalue);
                    $filterGids[] = m('goods_attr')->where("gtvalue='{$gtvalue}'")->getField('gid',true);
                }
            }
//            dd($filterGids);die;
            //如果有filterGids，说你那个param参数中有不是0
            if($filterGids)
            {
                $finalGids = $filterGids[0];
                foreach($filterGids as $k=>$v)
                {
                    $finalGids=array_intersect($finalGids,$v);
                }
                //跟$gids求交集，foreach循环param中if判断里面，找出来的gid可能不在我们当前gids范围
                $finalGids = array_intersect($finalGids,$gids);
            }else{
                //说明选的都是不限
                $finalGids = $gids;
            }
            //dd($filterGids);
        }else{
            //说明没有商品
            $finalGids = [];
        }
        //最后的商品id集合：$finalGids
        if($finalGids){
            //gid in (1,2)
            $data = m( 'goods' )->where( "gid in (".implode(',',$finalGids).")" )->select();
        }else{
            $data = [];
        }

//        $data=m('goods')->where("cid=$cid")->select();
//        dd($data);
        $this->assign('data',$data);

        $this->display();
    }

    public function getFather($data,$cid){
        static $temp=[];
        foreach ($data as $k=>$v){
            if ($v['cid']==$cid){
                $temp[] = $v;
                $this->getFather($data,$v['pid']);
            }

        }
        return $temp;

    }

    public function  getSon($data,$cid){
        static $temp=[];
        foreach ($data as $k=>$v){
            if($v['pid']==$cid){
                $temp[]=$v['cid'];
                $this->getSon($data,$v['cid']);
            }
        }
        return $temp;
    }

}